package com.hnust.controller;

import com.hnust.domain.Student;
import com.hnust.domain.record.*;
import com.hnust.service.StudentService;
import com.hnust.service.TokenService;
import com.hnust.util.IDUtil;
import com.hnust.util.PageVo;
import com.hnust.util.Result;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 *创建人：张汝往
 *创建时间：2020/10/6
 *描述：学生列表模块
 */
@CrossOrigin("*")
@RestController
@RequestMapping("/api/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private TokenService tokenService;

    /**
     * 页面初渲染时就获取年级数据
     * @param token token信息
     * @param collegeId 学院ID
     * @return
     */
    @GetMapping("/getGrade")
    public Result<List<String>> getGrade(
            @RequestParam(value = "token",required = true)String token,
            @RequestParam(value = "collegeId",required = true)String collegeId
    ){
        //System.out.println(token+'\n'+collegeId);
        if(tokenService.verifyToken(token,"院管理员")){
            List<String> grades=studentService.getAllGrade(collegeId);
            return new Result<List<String>>(1,"获取数据成功",grades);
        }else{
            return new Result<List<String>>(0,"没有操作权限,请登录",null);
        }
    }

    /**
     * 下拉点击年级下拉框时，在显示该年级内的所有专业
     * @param token token信息
     * @param collegeId 学院ID
     * @param grade 年级
     * @return
     */
    @GetMapping("/getMajor")
    public Result<List<String>> getMajor(
            @RequestParam(value = "token",required = true)String token,
            @RequestParam(value = "collegeId",required = true)String collegeId,
            @RequestParam(value = "grade",required = false)String grade
    ){
        if(tokenService.verifyToken(token,"院管理员")){
            List<String> majors=studentService.getAllMajor(grade,collegeId);
            return new Result<List<String>>(1,"获取数据成功",majors);
        }else{
            return new Result<List<String>>(0,"没有操作权限,请登录",null);
        }
    }

    /**
     * 下拉点击班级下拉框时，该年级专业内的所有班级
     * @param token token信息
     * @param collegeId 学院ID
     * @param grade 年级
     * @param major 专业
     * @return
     */
    @GetMapping("/getClass")
    public Result<List<String>> getClass(
            @RequestParam("token")String token,
            @RequestParam("collegeId")String collegeId,
            @RequestParam("grade")String grade,
            @RequestParam("major")String major
    ){
        if(tokenService.verifyToken(token,"院管理员")){
            List<String> classes=studentService.getAllClass(grade,major,collegeId);
            return new Result<List<String>>(1,"获取数据成功",classes);
        }else{
            return new Result<List<String>>(0,"没有操作权限,请登录",null);
        }
    }

    @GetMapping("/getClassNew")
    public Result<List<Map<String,String>>> getClassNew(
            @RequestParam("token")String token,
            @RequestParam("collegeId")String collegeId,
            @RequestParam("grade")String grade,
            @RequestParam("major")String major
    ){
        if(tokenService.verifyToken(token,"院管理员")){
            List<Map<String,String>> classes=studentService.getAllClassNew(grade,major,collegeId);
            return new Result<List<Map<String,String>>>(1,"获取数据成功",classes);
        }else{
            return new Result<List<Map<String,String>>>(0,"没有操作权限,请登录",null);
        }
    }
    /**
     * 点击学生列表时显示学院所有学生的信息
     * @param token token信息
     * @param collegeId 学院ID
     * @param rows 一行多少条数据
     * @param currentPage 当前页数
     * @return
     */
    @GetMapping("/msg")
    public Result<PageVo<List<SimpleStudentInfo>>> getSimpleStudentInfo(
            @RequestParam("token")String token,
            @RequestParam("collegeId")String collegeId,
            @RequestParam("rows")Integer rows,
            @RequestParam("currentPage")Integer currentPage
    ){
        if(tokenService.verifyToken(token,"院管理员")){
            PageVo<List<SimpleStudentInfo>> listPageVo=studentService.querySimpleStudenInfo(
                    collegeId,null,rows,currentPage
            );
            return new Result<PageVo<List<SimpleStudentInfo>>>(1,"获取数据成功",listPageVo);
        }else{
            return new Result<>(0,"没有操作权限,请登录",null);
        }
    }

    /**
     * 按条件查询学生
     * @param map 学生条件
     * @return
     */
    @PostMapping("/searchStu")
    public Result<PageVo<List<SimpleStudentInfo>>> searchSimpleStudentInfo(@RequestBody Map<String,Object> map){
        //验证token是否正确
        String token=map.get("token").toString();
        String collegeId=map.get("collegeId").toString();
        Integer rows=Integer.parseInt(map.get("rows").toString());
        Integer currentPage=Integer.parseInt(map.get("currentPage").toString());
        JSONObject jsonObject=JSONObject.fromObject(map.get("searchStudentInfo"));
        SearchStudentInfo searchStudentInfo=(SearchStudentInfo)  JSONObject.toBean(jsonObject, SearchStudentInfo.class);
        if(tokenService.verifyToken(token,"院管理员")){
            PageVo<List<SimpleStudentInfo>> listPageVo=studentService.querySimpleStudenInfo(
                    collegeId,searchStudentInfo,rows,currentPage
            );
            return new Result<PageVo<List<SimpleStudentInfo>>>(1,"获取数据成功",listPageVo);
        }else{
            return new Result<>(0,"没有操作权限,请登录",null);
        }
    }

    /**
     * 删除单条学生记录
     * @param token token信息
     * @param studentId 学生ID
     * @return
     */
    @GetMapping("/deleteStuRecord")
    public Result<Boolean> deleteStuRecord(
            @RequestParam("token")String token,
            @RequestParam("studentID")String studentId
    ){
        if(tokenService.verifyToken(token,"院管理员")){
            if(studentService.deleteStuRecord(studentId)){
                return new Result<Boolean>(1,"操作成功",null);
            }else{
                return new Result<Boolean>(0,"操作失败",null);
            }
        }else{
            return new Result<>(0,"没有操作权限,请登录",null);
        }
    }

    /**
     * 批量删除学生信息
     * @param map 返回前端数据（token，studentIDs）
     * @return
     */
    @PostMapping("/massDeleteStuRecord")
    public Result<Boolean> massDeleteStuRecord(@RequestBody Map<String, Object> map){
        String token=map.get("token").toString();
        JSONArray studentIDs= JSONArray.fromObject(map.get("studentIDs"));
        //System.out.println(studentIDs);
        if(tokenService.verifyToken(token,"院管理员")){
           if(studentService.massDeleteStuRecord(studentIDs)){
               return new Result<Boolean>(1,"操作成功",null);
           }else{
               return new Result<Boolean>(0,"操作失败",null);
           }
        }else{
            return new Result<>(0,"没有操作权限,请登录",null);
        }
    }

    /**
     * 重置学生密码与账号一致
     * @param token token信息
     * @param Id 学生ID
     * @return
     */
    @GetMapping("/resetStuPwd")
    public Result<Boolean> resetStuPwd(
            @RequestParam("token")String token,
            @RequestParam("studentID")String Id
    ){
        if(tokenService.verifyToken(token,"院管理员")){
            if(studentService.resetStuPwd(Id)){
                return new Result<Boolean>(1,"操作成功",null);
            }else{
                return new Result<Boolean>(0,"操作失败",null);
            }
        }else{
            return new Result<>(0,"没有操作权限,请登录",null);
        }
    }

    /**
     * 点击查看学生具体信息,获取学生具体信息
     * @param token token信息
     * @param stuId 学生ID
     * @return
     */
    @GetMapping("/getStudentInfo")
    public Result<StudentInfo> getStudentInfo(
            @RequestParam("token")String token,
            @RequestParam("studentID")String stuId
    ){
        //System.out.println("...."+token+"\n"+stuId);
        if(tokenService.verifyToken(token,"院管理员")){
            StudentInfo studentInfo=studentService.getStudentInfo(stuId);
            return new Result<StudentInfo>(1,"返回数据成功",studentInfo);
        }else{
            return new Result<>(0,"没有操作权限,请登录",null);
        }
    }

    @PostMapping("/addOneStudent")
    public Result<Boolean> addOneStudent(@RequestBody Map<String, Object> map){
        String token=map.get("token").toString();
        // System.out.println(student);
        if(tokenService.verifyToken(token,"院管理员")){
            try {
                String collegeId=map.get("collegeId").toString();
                JSONObject jsonObject = JSONObject.fromObject(map.get("student"));
                AddOneStudent student=(AddOneStudent) JSONObject.toBean(jsonObject, AddOneStudent.class);
                System.out.println(student);
                if (studentService.addOneStudent(student)) {
                    return new Result<Boolean>(1, "插入成功", null);
                } else {
                    return new Result<Boolean>(0, "插入失败", null);
                }
            }catch (Exception e){
                return new Result<Boolean>(0, "插入失败", null);
            }
        }else{
            return new Result<>(0,"没有操作权限,请登录",null);
        }
    }

}
